<?php defined('SYS_PATH') or die('No direct script access.');
/**
 * Logger class
 *
 * @author zendzhang<zendzhang@hotmail.com>
 * @version $Id: Logger.php 9 2013-01-14 03:24:50Z zendzhang@hotmail.com $
 */
class TrueBlood_Logger
{
    /**
     * Configuration
     *
     * @var array
     */
    protected $_config = array
    (
        'threshold'   => 4,
        'path'        => '',
        'date_format' => 'Y-m-d H:i:s'
    );

    const LEVEL_ERROR = 1;
    const LEVEL_DEBUG = 2;
    const LEVEL_INFO = 3;

    /**
     * Constructor
     */
    public function __construct()
    {
        $this->initialize();
    }

    /**
     * Logs error message
     *
     * @param string $message
     */
    public function error($message)
    {
        return $this->log($message, self::LEVEL_ERROR);
    }

    /**
     * Logs debug message
     *
     * @param string $message
     */
    public function debug($message)
    {
        return $this->log($message, self::LEVEL_DEBUG);
    }

    /**
     * Logs info message
     *
     * @param string $message
     */
    public function info($message)
    {
        return $this->log($message, self::LEVEL_INFO);
    }

    /**
     * Logs a message to the log file at the given level
     *
     * @param string $message
     * @param int $level
     */
    private function log($message, $level)
    {
        switch ($level)
        {
            case self::LEVEL_ERROR :
                $logLevel = 'ERROR';
                break;
            case self::LEVEL_DEBUG :
                $logLevel = 'DEBUG';
                break;
            case self::LEVEL_INFO :
                $logLevel = 'INFO';
                break;
            default :
                return false;
        }
        if ($level > $this->_config['threshold'])
        {
            return false;
        }
        $filepath = $this->_config['path'] . '/' . strtolower($logLevel) . '-' . date('Y-m-d') . '.php';
        $content = '';
        if (!file_exists($filepath))
        {
            $content .= "<" . "?php  defined('SYS_PATH') or die('No direct script access.'); ?" . ">\n\n";
        }
        $content .= $logLevel . ' - ' . date($this->_config['date_format']) . ' --> ' . trim($message) . "\n";
        return @error_log($content, 3, $filepath);
    }

    /**
     * Initialize preferences
     *
     * @return void
     */
    protected function initialize()
    {
        @include (APP_PATH . '/configs/log.inc.php');
        if (isset($config, $config['log']) && is_array($config['log']))
        {
            $this->_config = $config['log'] + $this->_config;
        }
        if (empty($this->_config['path']))
        {
            $this->_config['path'] = APP_PATH . '/logs';
        }
        else
        {
            $this->_config['path'] = rtrim($this->_config['path'], '\\/');
        }
        if (!is_dir($this->_config['path']))
        {
            @mkdir($this->_config['path'], 0666);
        }
    }

    /**
     * Magic method, calls a module method
     *
     * @param string $name
     * @return void
     */
    public function __call($method, $arguments)
    {
        $message = sprintf('Call to undefined function: %s::%s().', get_class($this), $method);
        $this->error($message);
        throw new TrueBlood_Exception($message);
    }
}

?>